package com.supply.dl.activity.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.FragmentPagerAdapter;
import com.supply.dl.widget.viewpagerindicator.PageIndicator;
import com.supply.dl.widget.viewpagerindicator.TabPageIndicator;

public abstract class ViewPagerFragment extends BaseFragment
{
	/**
	 * 是否选中后初始化
	 */
	protected boolean isInit = false;

	/**
	 * 是否选中
	 */
	private boolean isSelected = false;
	/**
	 * 自定义的参数
	 */
	protected Bundle customArguments = null;

	public boolean isInit()
	{
		return isInit;
	}

	protected void onPageSelected(boolean isSelected)
	{
		this.isSelected = isSelected;
		if (isSelected)
		{
			if (!isInit)
				initLoad();
			onPageSelected();
		}
	}

	public void setCustomArguments(Bundle customArguments)
	{
		this.customArguments = customArguments;
	}

	public Bundle getCustomArguments()
	{
		return customArguments;
	}

	/**
	 * ViewPager选中该Fragment
	 */
	protected void onPageSelected()
	{
	}

	public boolean isSelected()
	{
		return isSelected;
	}

	protected void initLoad()
	{
		isInit = true;
	}

	/**
	 * ViewPagerFragment的onPageSelected方法的工具类
	 */
	public static class ViewPagerFragmentUtils
	{
		/**
		 * 子线程与主线程的通信用的mHandler
		 */
		protected Handler mHandler = new Handler();

		private FragmentPagerAdapter fragmentPagerAdapter;

		private PageIndicator pageIndicator;

		private ViewPager viewPager;

		private int index;

		private ViewPagerFragment selectViewPagerFragment;

		/**
		 * 延迟选中指点的操作
		 */
		private Runnable selectRunnable = null;
		/**
		 * 延迟选中指点的操作
		 */
		private DelayedRunnable delayedRunnable = new DelayedRunnable();
		/**
		 * 设置没有选中的时候是否传递最新的参数过去
		 */
		private boolean notSelectHasData = false;

		public ViewPagerFragmentUtils(int index, FragmentPagerAdapter fragmentPagerAdapter, TabPageIndicator pageIndicator)
		{
			this(index, fragmentPagerAdapter, pageIndicator, null);
		}

		public ViewPagerFragmentUtils(int index, FragmentPagerAdapter fragmentPagerAdapter, PageIndicator pageIndicator, Bundle customArguments)
		{
			this.fragmentPagerAdapter = fragmentPagerAdapter;
			this.pageIndicator = pageIndicator;
			this.index = index;
			this.viewPager = pageIndicator.getViewPager();
			delayedRunnable.run();
		}

		public ViewPagerFragmentUtils(int index, FragmentPagerAdapter fragmentPagerAdapter, ViewPager viewPager)
		{
			this(index, fragmentPagerAdapter, viewPager, null);
		}

		public ViewPagerFragmentUtils(int index, FragmentPagerAdapter fragmentPagerAdapter, ViewPager viewPager, Bundle customArguments)
		{
			this.fragmentPagerAdapter = fragmentPagerAdapter;
			this.index = index;
			this.viewPager = viewPager;
			delayedRunnable.run();
		}

		public void setOnNotSelectHasData(boolean notSelectHasData)
		{
			this.notSelectHasData = notSelectHasData;
		}

		public void onPageSelected(int i)
		{
			onPageSelected(i, null);
		}

		public void onPageSelected(int i, Bundle customArguments)
		{
			this.index = i;
			delayedRunnable.customArguments = customArguments;
			mHandler.removeCallbacks(delayedRunnable);
			delayedRunnable.run();
		}

		private void setCurrentItem(int index)
		{
			this.index = index;
			if (pageIndicator != null)
				pageIndicator.setCurrentItem(index);
			else
			{
				if (viewPager != null)
					viewPager.setCurrentItem(index);
			}
		}

		/**
		 * 延迟选中指点的操作
		 */
		private class DelayedRunnable implements Runnable
		{
			Bundle customArguments;

			@Override public void run()
			{
				mHandler.removeCallbacks(selectRunnable);
				ViewPagerFragment fragment = (ViewPagerFragment) fragmentPagerAdapter.getFragment(viewPager, index);
				if (fragment != null && fragment.isAdded())
				{
//					if (selectViewPagerFragment != null)
//					{
//						selectViewPagerFragment.onPageSelected(false);
//					}
					setCurrentItem(index);
					selectRunnable = new SelectRunnable(fragment, customArguments);
					if (selectViewPagerFragment != fragment)
					{
						if (selectViewPagerFragment != null)
						{
							selectViewPagerFragment.onPageSelected(false);
						}
						selectViewPagerFragment = fragment;
					}
					if (notSelectHasData)
					{
						int length = fragmentPagerAdapter.getCount();
						for (int i = 0; i < length; i++)
						{
							if (i != index)
							{
								ViewPagerFragment notSelectFragment = (ViewPagerFragment) fragmentPagerAdapter.getFragment(viewPager, i);
								if (notSelectFragment != null)
								{
									notSelectFragment.setCustomArguments(customArguments);
									notSelectFragment.onPageSelected(false);
								}
							}
						}
					}
					mHandler.postDelayed(selectRunnable, 200);
				}
				else
				{
					mHandler.postDelayed(this, 100);
				}
			}
		}

		private class SelectRunnable implements Runnable
		{
			ViewPagerFragment viewPagerFragment;
			Bundle customArguments;

			public SelectRunnable(ViewPagerFragment viewPagerFragment, Bundle customArguments)
			{
				this.viewPagerFragment = viewPagerFragment;
				this.customArguments = customArguments;
			}

			@Override public void run()
			{
				if (viewPagerFragment != null && viewPagerFragment.isAdded())
				{
					viewPagerFragment.setCustomArguments(customArguments);
					viewPagerFragment.onPageSelected(true);
				}
			}
		}
	}


}
